diff --git a/src/compiler/crystal/compiler.cr b/src/compiler/crystal/compiler.cr
index b73465a0f132c9c74adef362ba926f4340770c34..dad0ae1134b2969fe21b050203525bed09dd9014 100644
--- a/src/compiler/crystal/compiler.cr
+++ b/src/compiler/crystal/compiler.cr
@@ -1,8 +1,9 @@
 require "option_parser"
 require "thread"
+require "io"
 
 lib C
-  fun tmpnam(result : UInt8*) : UInt8*
+  fun mkstemp(result : UInt8*) : Int32
 end
 
 module Crystal
@@ -112,7 +113,10 @@ module Crystal
         output_filename = @output_filename
       else
         if @run
-          output_filename = String.new(C.tmpnam(nil))
+          output_filename = "#{ENV["TMPDIR"] || "/tmp"}/.crystal-run.XXXXXX"
+          tmp_fd = C.mkstemp output_filename.cstr
+          raise "Error creating temp file #{output_filename}" if tmp_fd == -1
+          C.close tmp_fd
         else
           output_filename = File.basename(filename, File.extname(filename))
         end