Ruby: Could not find a temporary directory
Not sure what happened here, but I believe it had something to do with the /tmp folder permissions. I thought my /tmp folder was corrupted so I looked around about deleteing that folder and restoring it (I wasn't sure if this folder was especially significant about the way it was created). I found this source that suggested you can simply make the /tmp folder, just as you would any other folder, and then do a chmod 1777
on the newly created folder.
So, instead of deleting my current /tmp, I ran this chmod command and everything appeared to work.
What is strange to me is that I had previously done a chmod 777
and that caused the folder to not work. Weird...
Rails could not find a temporary directory (ArgumentError)
edit: As suggested in the comments just run
chmod +t /tmp
========
Old answer:
Your Ubuntu installation probably doesn't have TMPDIR
set. You should set that variable in your startup.
This link has more information on environment variables in Ubuntu: https://help.ubuntu.com/community/EnvironmentVariables
Change tmp folder for uploaded files
If setting the TMPDIR,TMP,TEMP is not working, it could be that the directory you specified doesn't exist or is not writable. Or $SAFE variable is > 0. The tmp folder is determined using the function Dir.tmpdir (see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tmpdir/rdoc/Dir.html#method-c-tmpdir).
class Dir
def Dir::tmpdir
tmp = '.'
if $SAFE > 0
tmp = @@systmpdir
else
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp']
if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
tmp = dir
break
end rescue nil
end
File.expand_path(tmp)
end
end
end
Ruby 2.1
def Dir::tmpdir
if $SAFE > 0
tmp = @@systmpdir
else
tmp = nil
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.']
next if !dir
dir = File.expand_path(dir)
if stat = File.stat(dir) and stat.directory? and stat.writable? and
(!stat.world_writable? or stat.sticky?)
tmp = dir
break
end rescue nil
end
raise ArgumentError, "could not find a temporary directory" if !tmp
tmp
end
end
So if you're setting the TMP env variables, make sure that the lines below are true
- $SAFE == 0
- File.stat("you_dir")
- File.stat("you_dir").directory?
- File.stat("you_dir").writable?
Another way to set tempdir is to override the tmpdir in your rails initializer, but obviously this bypasses any directory checking so u gotta make sure it exists/writable
class Dir
def self.tmpdir
"/your_directory/"
end
end
Specify a non-standard temporary directory for open() method in Ubuntu
Looking at the OpenURI
source code we can see that it uses Tempfile
:
[...]
io = Tempfile.new('open-uri')
[...]
Tempfile
in order to choose the temporary directory uses Dir.tmpdir
, which in turn uses the system temporary directory or a directory specified by the environment variable TMPDIR
(between others). So we can write something like this:
require 'open-uri'
require 'fileutils'
d = "#{Dir.home}/.tmp"
Dir.mkdir d
ENV["TMPDIR"] = d
p open("http://www.google.com")
ENV.delete("TMPDIR")
FileUtils.rm_rf d
In a single command (please ensure that $HOME/.tmp
does not exist and is not used):
ruby -ropen-uri -rfileutils -e 'd = "#{Dir.home}/.tmp"; Dir.mkdir d; ENV["TMPDIR"] = d; p open("http://www.google.com"); ENV.delete("TMPDIR"); FileUtils.rm_rf d'
It should print something like
#<Tempfile:$HOME/.tmp/open-uri20131115-16887-nag9pr>
P.S. I'm using Ruby 2.1.0 preview, so maybe you have to look at #{ruby directory}/lib/ruby/2.0.0/open-uri.rb
source in order to understand how OpenURI
manages the temporary file (but it should be very similar)
What is the best way to get an empty temporary directory in Ruby on Rails?
The Dir#tmpdir
function in the Ruby core (not stdlib that you linked to) should be cross-platform.
To use this function you need to require 'tmpdir'
.
Change temporary directory in Rails 4
It seems that some libraries like to hard code values... See: [1]
By adding the following you can get around the hard coded value:
config.assets.cache_limit = 50.megabytes
config.assets.configure do |env|
env.cache = Sprockets::Cache::FileStore.new(
File.join(ENV['RAILS_TMP'], 'cache/assets'),
config.assets.cache_limit,
env.logger
)
end
Related Topics
How to Send an Http Put Request in Ruby
Unpermitted Parameters for Dynamic Forms in Rails 4
Is Systemexit a Special Kind of Exception
Rails 3.1 Load CSS in Particular Order
How to Access Cookies from Applicationcontroller (Rails)
Why Does Rails 4.2 + Responders Keeps Telling Me to Add Responders to the Gemfile
Rvm Ruby with Tk Installation (Osx)
Is There a Ruby Method That Just Returns the Value of a Block
Installing Ruby-2.1.2: Cannot Load Such File -- Openssl (Loaderror)
Ruby on Rails Multiple Http Request at the Same Time
How to Find If Range Is Contained in an Array of Ranges
How Can Bundler/Gemfile Be Configured to Use Different Gem Sources During Development
Rails 3.1 - Changing Default Scaffold Views and Template
Symbol#To_Proc with Custom Methods
Converting a Unique Seed String into a Random, Yet Deterministic, Float Value in Ruby